package com.cnbot.irobotvoice.api;

import android.util.Log;

import com.cnbot.irobotvoice.listener.IRobotTtsResultListener;
import com.xiaoi.why.sdk.utils.callback.TTSCallback;
import com.xiaoi.why.sdk.vcloud.tts.TTSManager;

/**
 * Copyright (c) 2016--2019/2/21  Hunan Cnbot Co., Ltd. All Rights Reserved.
 *
 * @descriptoin 小i，tts
 * @FileName: IRoboTtsTools.java
 * @author: dc
 * @date: 2019/2/21 11:40
 * @version: 1.0
 */

public class IRoboTtsTools {
    private final String TAG = IRoboTtsTools.class.getSimpleName();

    /**
     * 小i tts引擎
     */
    private TTSManager ttsManager;

    /**
     * 小i ，合成结果监听器
     */
    private IRobotTtsResultListener iRobotTtsResultListener;

    private static IRoboTtsTools instance;


    /**
     * 正式运行服务器地址
     * http://10.255.30.97/synth.do
     */
    String RELEASE_BASE_TTS_URL = "10.255.30.97";
    /**
     * http://vcloud.xiaoi.com/synth.do
     * debug运行服务器地址
     */
    String DEBUG_BASE_TTS_URL = "vcloud.xiaoi.com";

    /**
     * 服务器地址, 根据buildConfig的变量DEBUG_SERVER_URL 获取不同的服务器地址
     */
    String TTS_URL;

    public IRoboTtsTools(boolean isDebug) {
        TTS_URL = isDebug == false ? "http://" + RELEASE_BASE_TTS_URL + "/synth.do" : "http://" + DEBUG_BASE_TTS_URL + "/synth.do";
    }


    public static IRoboTtsTools getInstance(boolean isDebug){
        if(instance == null){
            synchronized (IRoboTtsTools.class){
                if(instance == null){
                    instance = new IRoboTtsTools(isDebug);
                }
            }
        }
        return instance;
    }

    /**
     * 初始化tts
     */
    public void initTts(){
        ttsManager = new TTSManager.Builder()
                .appKey("9516d09e23e34ae38687fc6b2e038d7c")
                .appSecret("0fd08ffd50944af5811cc1412d682a2e")
                .url(TTS_URL)
                .build();
        Log.e(TAG, "TTS_URL = " + TTS_URL);
    }


    /**
     * 注册tts监听器
     * @param iRobotTtsResultListener
     */
    public void setiRobotTtsResultListener(IRobotTtsResultListener iRobotTtsResultListener) {
        this.iRobotTtsResultListener = iRobotTtsResultListener;
    }

    /**
     * 注销tts监听器
     */
    public void removeiRobotTtsResultListener() {
        this.iRobotTtsResultListener = null;
    }

    /**
     * 开始一次语音合成请求并查看回调结果
     */
    public void startTts(String text){
        if(ttsManager != null){
            ttsManager.startTTS(text, new IRobotTtsResult());
        }
    }

    /**
     * 用于结束正在播报的语音
     */
    public void stopTts(){
        if(ttsManager != null){
            ttsManager.stopTTS();
        }
    }


    /**
     * 语音合成结果
     */
    public class IRobotTtsResult implements TTSCallback{

        @Override
        public void onTTSStart() {
            Log.e(TAG, "onTTSStart: 开始TTS   Thread: " + Thread.currentThread().getName());
            if(iRobotTtsResultListener != null){
                iRobotTtsResultListener.ttsBefore();
            }
        }

        @Override
        public void onTTSEnd() {
            Log.e(TAG, "onTTSEnd: 结束TTS   Thread: " + Thread.currentThread().getName());
            if(iRobotTtsResultListener != null){
                iRobotTtsResultListener.ttsEnd();
            }
        }

        @Override
        public void onTTSError(String s) {
            Log.e(TAG, "onTTSError: TTS报错 " + s +  " Thread:  " + Thread.currentThread().getName());
            if(iRobotTtsResultListener != null){
                iRobotTtsResultListener.ttsError(s);
            }
        }
    }
}
